import { Game, GameComponent } from "@sapi-game/main";
import { HideAndSeekMainState } from "../mainState";
import { EffectIds } from "@sapi-game/utils/vanila-data";
import { HideAndSeekRespawn } from "../components/respawn";
import { SeekerEquipment } from "../components/seekerEquip";
import { HideAndSeekRole } from "../player";

export class HideAndSeekInfectionMode extends GameComponent<HideAndSeekMainState> {
    override onAttach(): void {
        this.context.players.title("§c游戏开始", "§b§l追捕者将在15s后产生");

        const equipComp = this.state.getComponent(SeekerEquipment);
        //延迟开始
        this.runner.runDelay(() => {
            this.state.drawSeeker();
            const names = this.context.players
                .filter((p) => p.role == HideAndSeekRole.Seeker)
                .map((p) => p.name);
            this.context.players.title(
                "§c§l" + names.join(","),
                "§b§l成为了追捕者"
            );
            equipComp.loadEquipForAll(); //给装备
            this.state.start();
            this.subRespawn();
        }, 300);
    }

    subRespawn() {
        //订阅死亡事件
        this.state.getComponent(HideAndSeekRespawn).sub((p, type) => {
            if (type == "catch") {
                p.role = HideAndSeekRole.Seeker;
                this.context.players.sendMessage(
                    `§c${p.name} §l的烤肠被抢走了,并变成了追捕者`
                );
            }
        });
    }

    giveEffect() {
        this.context.players.forEach((p) => {
            if (p.role == HideAndSeekRole.Hider) {
                p.player.addEffect(EffectIds.Invisibility, 200, {
                    showParticles: false,
                });
                p.player.addEffect(EffectIds.Speed, 100, {
                    showParticles: false,
                });
            }
            //抓捕者
            if (p.role == HideAndSeekRole.Seeker) {
                p.player.addEffect(EffectIds.Blindness, 100, {
                    showParticles: false,
                });
                p.player.addEffect(EffectIds.Slowness, 100, {
                    showParticles: false,
                });
            }
        });
    }
}
